<% set_title(t(".title", :id => @note.id)) %>

<%= render "sidebar_header", :title => t(".#{@note.status}_title", :note_name => @note.id) %>

<div>
  <h4><%= t(".description") %></h4>
  <div class="overflow-hidden ms-2">
    <%= note_description(@note.author, @note.description, current_user&.moderator? ? @note.comments.unscope(:where => :visible).first : @note.comments.first).to_html %>
  </div>

  <div class="details" data-coordinates="<%= @note.lat %>,<%= @note.lon %>" data-status="<%= @note.status %>">
    <p class="mb-2"><%= note_event("opened", @note.created_at, @note.author) %></p>
    <% if @note.status == "closed" %>
      <p class="mb-2"><%= note_event(@note.status, @note.closed_at, @note.all_comments.last.author) %></p>
    <% end %>
    <p>
      <%= t "browse.location" %>
      <%= link_to t(".coordinates_html",
                    :latitude => tag.span(number_with_delimiter(@note.lat), :class => "latitude"),
                    :longitude => tag.span(number_with_delimiter(@note.lon), :class => "longitude")),
                  root_path(:anchor => "map=18/#{@note.lat}/#{@note.lon}") %>
    </p>
  </div>

  <% if @note_includes_anonymous -%>
    <p class='alert alert-warning'><%= t ".anonymous_warning" %></p>
  <% end -%>

  <div class="row">
    <div class="col">
      <h4><%= t(".discussion") %></h4>
    </div>

    <% if current_user %>
      <div class="col-auto">
        <% if @note.subscribers.exists?(current_user.id) %>
          <%= tag.button t(".unsubscribe"),
                         :type => "button",
                         :class => "btn btn-sm btn-primary",
                         :name => "unsubscribe",
                         :data => { :method => "DELETE",
                                    :url => api_note_subscription_path(@note) } %>
        <% else %>
          <%= tag.button t(".subscribe"),
                         :type => "button",
                         :class => "btn btn-sm btn-primary",
                         :name => "subscribe",
                         :data => { :method => "POST",
                                    :url => api_note_subscription_path(@note) } %>
        <% end %>
      </div>
    <% end %>
  </div>

  <% @note_comments.each do |comment| %>
    <article id="c<%= comment.id %>">
      <small class='text-body-secondary'><%= note_event(comment.event, comment.created_at, comment.author) %></small>
      <div class="mx-2">
        <%= comment.body.to_html %>
      </div>
    </article>
  <% end %>

  <% if @note.status == "open" %>
    <% if current_user -%>
      <form class="mb-3" action="#">
        <div class="mb-3">
          <textarea class="form-control" name="text" cols="40" rows="5" maxlength="2000"></textarea>
        </div>
        <div id="comment-error" class="alert alert-danger p-2 mb-3" hidden>
        </div>
        <div class="d-flex flex-wrap gap-1">
          <% if current_user.moderator? -%>
            <%= button_tag t(".hide"), :name => "hide", :class => "btn btn-light",
                                       :data => { :method => "DELETE",
                                                  :url => api_note_url(@note, "json") } %>
          <% end -%>
          <%= button_tag t(".resolve"), :name => "close", :class => "btn btn-primary",
                                        :data => { :method => "POST",
                                                   :url => close_api_note_url(@note, "json"),
                                                   :default_action_text => t(".resolve"),
                                                   :comment_action_text => t(".comment_and_resolve") } %>
          <%= button_tag t(".comment"), :name => "comment", :class => "btn btn-primary", :disabled => true,
                                        :data => { :method => "POST",
                                                   :url => comment_api_note_url(@note, "json") } %>
        </div>
      </form>
    <% else -%>
      <p>
        <%= link_to t(".log_in_to_comment"), login_path(:referer => request.fullpath) %>
      </p>
    <% end -%>
  <% else %>
    <form class="mb-3" action="#">
      <input type="hidden" name="text" value="" autocomplete="off">
      <div id="comment-error" class="alert alert-danger p-2 mb-3" hidden>
      </div>
      <div class="d-flex flex-wrap gap-1">
        <% if @note.status != "hidden" and current_user and current_user.moderator? -%>
          <%= button_tag t(".hide"), :name => "hide", :class => "btn btn-light",
                                     :data => { :method => "DELETE",
                                                :url => api_note_url(@note, "json") } %>
        <% end -%>
        <% if current_user -%>
          <%= button_tag t(".reactivate"), :name => "reopen", :class => "btn btn-primary",
                                           :data => { :method => "POST",
                                                      :url => reopen_api_note_url(@note, "json") } %>
        <% end -%>
      </div>
    </form>
  <% end %>

  <% if current_user && current_user != @note.author %>
    <p>
      <small class="text-body-secondary">
        <%= t ".report_link_html", :link => report_link(t(".report"), @note) %>
        <% if @note.status == "open" %>
          <%= t ".other_problems_resolve", :link => report_link(t(".report"), @note) %>
        <% elsif @note.status == "closed" %>
          <%= t ".other_problems_resolved" %>
        <% end %>
      </small>
    </p>
  <% end %>

  <% if @note.freshly_closed? %>
    <small class="text-body-secondary">
      <%= t ".disappear_date_html", :disappear_in => friendly_date(@note.freshly_closed_until) %>
    </small>
  <% end %>
</div>
